<?php

require_once dirname(__FILE__) . '/TextPreprocess.php';
require_once dirname(__FILE__) . '/ImagePreprocess.php';
require_once dirname(__FILE__) . '/VideoPreprocess.php';

final class Preprocessor
{
    public static function getContent($type, $content)
    {
        if (is_null($content))
        {
            throw new Exception('the content cannot be empty ' . __FUNCTION__, ErrorDef::COMMON_ERROR_PARAM);
        }
        
        $processor = self::preprocessorFactory($type);
        $ret = $processor->getContent($content);
        return $ret;
    }
    
    public static function parseContent($type, $content)
    {
        if (is_null($content))
        {
            throw new Exception('the content cannot be empty ' . __FUNCTION__, ErrorDef::COMMON_ERROR_PARAM);
        }
        
        $processor = self::preprocessorFactory($type);
        $ret = $processor->parseContent($content);
        return $ret;
    }
    
    public static function editContent($type, $oldContent, $content)
    {
        if (is_null($content) || is_null($oldContent))
        {
            throw new Exception('the old content or content cannot be empty ' . __FUNCTION__, ErrorDef::COMMON_ERROR_PARAM);
        }
        $processor = self::preprocessorFactory($type);
        $ret = $processor->editContent($content, $oldContent);
        return $ret;
    }
    
    private static function preprocessorFactory($type)
    {
        $processor = null;
        switch($type)
        {
            case PostTypeDef::TEXT:
                $processor = new TextPreprocess();              
                break;
            case PostTypeDef::IMAGE:
                $processor = new ImagePreprocess();
                break;
            case PostTypeDef::VIDEO:
                $processor = new VideoPreprocess();
                break;
            case PostTypeDef::AUDIO:
            case PostTypeDef::LINK:
            default:
                throw new Exception('the type[' . $type . ' is not supported in ' . __FUNCTION__, ErrorDef::COMMON_ERROR_PARAM);
                break;
        }
        
        return $processor;
    }
}